﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPI.Models;

namespace GPI.Controllers
{ 
    public class DateController : Controller
    {
        private Entities DB;
        private Entities db
        {
            get
            {
                if (DB == null)
                {
                    DB = Entities.GetEntitiesForUser(User);
                }
                return DB;
            }
        }
        //
        // GET: /Date/

        public ViewResult Index()
        {
            ViewBag.CanEdit = CanEdit();
            return View(db.GPI_PERIOD.ToList());
        }

        private bool CanEdit()
        {
            GPI_PERIOD periode = db.GPI_PERIOD.Find(1);
            DateTime now = System.DateTime.Now;
            DateTime start = periode.start_date;
            DateTime end = periode.end_date;
            if (now > start && now < end)
                return false;
            return true;
        }

        //
        // GET: /Date/Details/5

        public ViewResult Details(int id)
        {
            GPI_PERIOD gpi_period = db.GPI_PERIOD.Find(id);
            return View(gpi_period);
        }

        //
        // GET: /Date/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Date/Create

        [HttpPost]
        public ActionResult Create(GPI_PERIOD gpi_period)
        {
            if (ModelState.IsValid)
            {
                db.GPI_PERIOD.Add(gpi_period);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(gpi_period);
        }
        
        //
        // GET: /Date/Edit/5
 
        public ActionResult Edit(int id)
        {
            GPI_PERIOD gpi_period = db.GPI_PERIOD.Find(id);
            return View(gpi_period);
        }

        //
        // POST: /Date/Edit/5

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult Edit(GPI_PERIOD gpi_period)
        {
            gpi_period.PK_Period = 1;
            if (ModelState.IsValid)
            {
                db.Entry(gpi_period).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(gpi_period);
        }

        //
        // GET: /Date/Delete/5
 
        public ActionResult Delete(int id)
        {
            GPI_PERIOD gpi_period = db.GPI_PERIOD.Find(id);
            return View(gpi_period);
        }

        //
        // POST: /Date/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            GPI_PERIOD gpi_period = db.GPI_PERIOD.Find(id);
            db.GPI_PERIOD.Remove(gpi_period);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}